package com.hkemmm.alipay.config;


import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.hkemmm.alipay.bean.AliPayBean;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
public class Alipay {

    /**
     * 日志对象
     */
    private static final Logger logger = LoggerFactory.getLogger(Alipay.class);

    private final String format = "json";

    /**
     * appId
     */
    @Value("2021000119642406")
    private String appId;

    /**
     * 商户私钥
     */
    @Value("MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCP28RMlBU4RWeBL6Yf6gr6QPxEROfhSijoUJiX5lu5qPdm9UbKM5Ho3PLtRmoavQwvxlefWns3w/BPgSJjKHaWDk1n9xRuzEcUs4JYKU0GHpk4kSgmGXgwJO13dJ2khRU7BdKvTDcJHs2hJ4CJyK2cU7oLO5utgmPaOPn9uBmZsNP9U0B3Lsj8UGhwgMTdGNG3T+9l1bFNynOseSgg/mVioxhc9Eqo8KZFjjCntEbyK5qUwgaVVrZz5r/o6OL9mvMxUfhpwkSbmYKCJRcYixz5Y5tyTs4lM2m3U3M7ZN3q5yO+42aYjPipIlB+rSjou7hNnC2lSOwfTGhC5iGf0oilAgMBAAECggEAWnRdIambDh7Vzi0fz8Fh1ak29dY91zLCNhE3YYu+8SaFKX/gWxPCEyGavgcggF+OYhvczJ4gDBhv6g3QiXjP19p2YYDCoEhiCWo18ylE/SwQO7lJ/WrWX69YgVwaI3BqwQoQR5ksodd0hHhKIljEkR80RAve/2KVVoms5xPzROcwQxEehHJ0yHq2G/1yDn3AUCyZarsgxFuzN9KBPCKxY5HaKr+lrzsD9S5N6xMvfbReUbmkK10aGqYBiaX93lgCjiWjOKdRKFd3LV5kpoPiOIbnspuzHJ9K/6URq/rbge88330pl7Lkkt5848Ix3sHh62Uuxhkg3vXJ6XT6plqIAQKBgQDU2kRJeoFMFBOcNaYCwvt7g5mJAO/06bKZvYsiU0iBmIPpoLlNZnjohweaDkTvme3tHv7Uh9xTeXS4+R1grHxwDlp2uXUEzEXKHeuV59ngUFb9iJetBDvQlbatJetFzaOpZlIOpzYD51HfUhEtnTeScH2H9ROZGLb0b6rA7N4v5QKBgQCtBSGK21+NVt7synZ6QyrQBwCj+bfKzpIQYbL5PPZdafvPlSfaOo+tJL4h8nBrK0n6WzkxuS42sq3jdCWEYJa6kAFjYgKKT5i0olBaiyQvthA+VQtVdb0nhpm745lBdYMZo7M7SS4G9fqwFE/oNGIAG/6gpfiMejE0NwjbokXpwQKBgQC9bD0HMylr3hYRl0DVBlatddEmNppVV8CkxyTR+FH3WxZRzMH7mq0RKlHejMJWhKNTuZjI5NtzwgvZLYEybm4TfwKM08DK80tMctIcRwgDcxvnZy6sP69HkmTOtIcrSLVUZQWzmqUcVjjrR+qXsGkrPkbIvpahUXhIEjRgmquk7QKBgDndQ3CLGZPNf9S1iZYuLH1SOjac54jeOR9dbVvJtaNcNKkh+ygS+cYwSZ5U1KAi5CTkqWYlxnzomRs+1uVE4GQYkT5Jowvavvl6Z2FAs6N5Qkl2a8nzDIXRkEy0O6cWw/epNeIYAVq2nVaSFH9WNYhP1IKpv0xL+XS7Cy1ZQgSBAoGBAJnz23PaItdpq916uenApcvxhLwWXto59AugblpS8t360lzREpPtxoqCtXDtnK/dwhhmPIanxGun9B4a+PXRiBa76fCxUotLVpj6FEpRrCLT74FrNjiDbH2xav7tCIFxwFFhK1k1xAMMM7QOVS21ehLw3EbcDmX23RsnMiTEVhtJ")
    private String privateKey;

    /**
     * 支付宝公钥
     */
    @Value("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiyjkiNslqJ4x79aqLH3rU0LMsS/xO2EF6SbTDrdjTiCIO1tLjMrKhXE1gwuMkeTQRU7dxC22FsYLm5UdqD5CLdF8cnFhuAKOkHm/PMgcc6SIkg0GOBIymocaQaOEPJiLyWI1um3P/NCWtGZiiFdolj9gGvuZSLkDnX4gUXuheyZz5EwCNMym80EZOFNMn/CTORl+aZdgJC4NSmYJiiiMynXmfG0sA3lNmygpLLCKYaeUadtkh5lEXjcQQV2fhXSwILrVMeeCKvSVyE6hrX8wE2ETR8Kr/7f3IaZdcekys1TdW240jPdIn2q6KRHexPbPuvFL75Rp2I3Wv1xerbgjTwIDAQAB")
    private String publicKey;

    /**
     * 服务器异步通知页面路径，需要公网能访问到
     */
    @Value("http://localhost:9095/err.jsp")
    private String notifyUrl;

    /**
     * 服务器同步通知页面路径，填写自己的成功页面路径
     */
    @Value("http://localhost:9095/jump/ok")
    private String returnUrl;

    /**
     * 签名方式
     */
    @Value("RSA2")
    private String signType;

    /**
     * 字符编码格式
     */
    @Value("utf-8")
    private String charset;

    /**
     * 支付宝网关
     */
    @Value("https://openapi.alipaydev.com/gateway.do")
    private String gatewayUrl;

    public String pay(AliPayBean aliPayBean) throws AlipayApiException {

        AlipayClient alipayClient = new DefaultAlipayClient(
                gatewayUrl, appId, privateKey, format, charset, publicKey, signType);

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);
        alipayRequest.setBizContent(JSON.toJSONString(aliPayBean));
        logger.info("封装请求支付宝付款参数为:{}", JSON.toJSONString(alipayRequest));

        String result = alipayClient.pageExecute(alipayRequest).getBody();
        logger.info("请求支付宝付款返回参数为:{}", result);

        return result;
    }
}
